package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.*;
import com.zhongyu.pojo.*;
import com.zhongyu.service.OrderService;
import com.zhongyu.service.UserService;
import com.zhongyu.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("OrderService")
public class OrderServicelmpl extends ServiceImpl<OrderMapper,Order> implements OrderService {
    @Resource
    private  GoodsMapper goodsMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private UserMapper userMapper;
  @Resource
  private ProviderMapper providerMapper;
  @Resource
  private KehuMapper kehuMapper;
@Resource OrderDetailMapper orderDetailMapper;
    private  String getEmpName(Integer userId,Map<Integer,String> nameMap){
        if (userId==null){
            return  null;
        }
        String   username=nameMap.get(userId);
 if (username==null){
     username= userMapper.selectById1(userId);
             nameMap.put(userId,username);
        }
        return  username;
    }
    private  String getProviderName(Integer providerId,Map<Integer,String> providernameMap){
        if (providerId==null){
            return  null;
        }
        String   proName=providernameMap.get(providerId);
        if (proName==null){
            proName= providerMapper.findById(providerId);
            providernameMap.put(providerId,proName);
        }
        return  proName;
    }
    private  String getKehuName(Integer kehuId,Map<Integer,String> kehunameMap){
        if (kehuId==null){
            return  null;
        }
        String   proName=kehunameMap.get(kehuId);
        if (proName==null){
            proName= kehuMapper.findById(kehuId);
            kehunameMap.put(kehuId,proName);
        }
        return  proName;
    }
    /**
     * 销售
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageResult searchAll(Integer pageNo, Integer pageSize, XsOrderVo xsOrderVo) {
            LambdaQueryWrapper<Order> wrapper= Wrappers.<Order>lambdaQuery();
            wrapper.eq(xsOrderVo.getId()!=null,Order::getId,xsOrderVo.getId())
                    .ge(xsOrderVo.getStartDatet()!=null,Order::getStockTime,xsOrderVo.getStartDatet())
                    .le(xsOrderVo.getEndDatet()!=null,Order::getStockTime,xsOrderVo.getEndDatet())
                    .eq(xsOrderVo.getState()!=null,Order::getState,xsOrderVo.getState())
                    .eq(xsOrderVo.getType()!=null,Order::getType,xsOrderVo.getType())
                    .eq(xsOrderVo.getProviderId()!=null,Order::getProviderId,xsOrderVo.getProviderId())
                    .eq(xsOrderVo.getCreator()!=null,Order::getCreator,xsOrderVo.getCreator())
                    .eq(xsOrderVo.getStockKeeper()!=null,Order::getStockKeeper,xsOrderVo.getStockKeeper())
                    .ge(xsOrderVo.getStartDate()!=null,Order::getCreateTime,xsOrderVo.getStartDate())
                    .le(xsOrderVo.getEndDate()!=null,Order::getCreateTime,xsOrderVo.getEndDate());
            Page page=new Page(pageNo,pageSize);
        List<Order> list=baseMapper.findAll(page,wrapper);
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        Map<Integer,String> kehunameMap=new HashMap<Integer,String>();
        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
            order.setProviderIdName(getKehuName(order.getProviderId(),kehunameMap));

        }
        return new PageResult(list,page.getTotal());
    }

    /**
     * 根据销售id查
     * @param id
     * @return
     */
    @Override
    public List<Order> searchById(Integer id) {
        List<Order> list=orderMapper.findById(id);
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        Map<Integer,String> kehunameMap=new HashMap<Integer,String>();
        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
            order.setProviderIdName(getKehuName(order.getProviderId(),kehunameMap));
        }
        return list;
    }




    /**
     * 未出库销售订单查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageResult search1(Integer pageNo, Integer pageSize) {
        Page page=new Page(pageNo,pageSize);
        List<Order> list=baseMapper.findAll2(page);
        Map<Integer,String> kehunameMap=new HashMap<Integer,String>();
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
            order.setProviderIdName(getKehuName(order.getProviderId(),kehunameMap));

        }
        return new PageResult(list,page.getTotal());
    }

    /**
     * 添加销售订单
     * @param order
     */
    @Override
    public ResultVo xsadd(Order order) {
        order.setCreateTime(LocalDateTime.now());
        order.setType(2);
        order.setState(String.valueOf(0));
        double  totalMoney=0;
        orderMapper.insert(order);
        for(OrderDetail detail : order.getOrderDetail()){
            if(detail.getGoodsId()!=null&&detail.getGoodsId()>0){
             if (detail.getNum()!=null&&detail.getNum()>0) {
                 detail.setMoney(detail.getNum()*detail.getPrice());
                 detail.setState(Integer.valueOf(order.getState()));
                 detail.setOrderId(order.getId());
              Goods goods= goodsMapper.findById(detail.getGoodsId());
                 if (detail.getGoodsName().equals(goods.getName())) {
                     orderDetailMapper.insert(detail);
                     totalMoney += detail.getMoney();
                 }else{
                     return ResultVo.failure("商品信息不匹配");
                 }
             }else{
                 orderMapper.deleteById(order.getId());
                 return ResultVo.failure("数量不能为空且要大于零");
             }
        }else{
                orderMapper.deleteById(order.getId());
                return ResultVo.failure("商品信息不完整");
            }
        }
        order.setTotalMoney(totalMoney);
        orderMapper.updateById(order);
        return  ResultVo.success("添加销售订单成功");
    }



    /**
     * 采购
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageResult search(Integer pageNo, Integer pageSize, CgOrderVo cgOrderVo) {
        LambdaQueryWrapper<Order> wrapper= Wrappers.<Order>lambdaQuery();
        wrapper .eq(cgOrderVo.getType()!=null,Order::getType,cgOrderVo.getType())
                .eq(cgOrderVo.getState1()!=null,Order::getState,cgOrderVo.getState1())

                .or()
                .eq(cgOrderVo.getType()!=null,Order::getType,cgOrderVo.getType())
                .eq(cgOrderVo.getState()!=null,Order::getState,cgOrderVo.getState())


                .ge(cgOrderVo.getStartDate1()!=null,Order::getCreateTime,cgOrderVo.getStartDate1())
                .le(cgOrderVo.getEndDate1()!=null,Order::getCreateTime,cgOrderVo.getEndDate1())
                .ge(cgOrderVo.getStartDate2()!=null,Order::getCheckTime,cgOrderVo.getStartDate2())
                .le(cgOrderVo.getEndDate2()!=null,Order::getCheckTime,cgOrderVo.getEndDate2())
                .ge(cgOrderVo.getStartDate3()!=null,Order::getPurchaseTime,cgOrderVo.getStartDate3())
                .le(cgOrderVo.getEndDate3()!=null,Order::getPurchaseTime,cgOrderVo.getEndDate3())
                .ge(cgOrderVo.getStartDate4()!=null,Order::getStockTime,cgOrderVo.getStartDate4())
                .le(cgOrderVo.getEndDate4()!=null,Order::getStockTime,cgOrderVo.getEndDate4())
                .eq(cgOrderVo.getProviderId()!=null,Order::getProviderId,cgOrderVo.getProviderId())
                .eq(cgOrderVo.getCreator()!=null,Order::getCreator,cgOrderVo.getCreator())
                .eq(cgOrderVo.getStockKeeper()!=null,Order::getStockKeeper,cgOrderVo.getStockKeeper())
                .eq(cgOrderVo.getChecker()!=null,Order::getChecker,cgOrderVo.getChecker())
                .eq(cgOrderVo.getPurchaser()!=null,Order::getPurchaser,cgOrderVo.getPurchaser())
                .eq(cgOrderVo.getState()!=null,Order::getState,cgOrderVo.getState())
                .eq(cgOrderVo.getId()!=null,Order::getId,cgOrderVo.getId());


        Page page=new Page(pageNo,pageSize);
        List<Order> list=baseMapper.findAll1(page,wrapper);
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        Map<Integer,String> providernameMap=new HashMap<Integer,String>();

        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
            order.setProviderIdName(getProviderName(order.getProviderId(),providernameMap));

        }
        return new PageResult(list,page.getTotal());
    }

    @Override
    public void update(Order order) {
         baseMapper.updateById(order);
    }

    @Override
    public List<Order> searchOrder(Integer id) {
        return baseMapper.findAllOrder(id);
    }

    @Override
    public ResultVo tianCai(Order order) {
        order.setCreateTime(LocalDateTime.now());
        order.setType(1);
        order.setState(String.valueOf(0));
        double  totalMoney=0;
        orderMapper.insert(order);
        for(OrderDetail detail : order.getOrderDetail()){
            if(detail.getGoodsId()!=null&&detail.getGoodsId()>0){
                if (detail.getNum()!=null&&detail.getNum()>0) {
                    detail.setMoney(detail.getNum()*detail.getPrice());
                    detail.setState(Integer.valueOf(order.getState()));
                    detail.setOrderId(order.getId());
                    Goods goods= goodsMapper.findById(detail.getGoodsId());
                    if (detail.getGoodsName().equals(goods.getName())) {
                        orderDetailMapper.insert(detail);
                        totalMoney += detail.getMoney();
                    }else{
                        return ResultVo.failure("商品信息不匹配");
                    }
                }else{
                    orderMapper.deleteById(order.getId());
                    return ResultVo.failure("数量不能为空且要大于零");
                }
            }else{
                orderMapper.deleteById(order.getId());
                return ResultVo.failure("商品信息不完整");
            }
        }
        order.setTotalMoney(totalMoney);
        orderMapper.updateById(order);
        return  ResultVo.success("添加采购订单成功");
    }

    @Override
    public void del(Integer id) {
        orderMapper.deleteById(id);
    }

    @Override
    public List<Order> searchCC(Integer id) {
        List<Order> list=baseMapper.findAllCC(id);
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
            order.setProviderIdName(getEmpName(order.getProviderId(),nameMap));
        }
        return list;
    }


/*    @Override
    public List<Order> search2(Integer id) {
        List<Order> list=baseMapper.findAll12(id);
        Map<Integer,String> nameMap=new HashMap<Integer,String>();
        for (Order order:list){
            order.setCreatorName(getEmpName(order.getCreator(),nameMap));
            order.setCheckerName(getEmpName(order.getChecker(),nameMap));
            order.setPurchaserName(getEmpName(order.getPurchaser(),nameMap));
            order.setStockKeeperName(getEmpName(order.getStockKeeper(),nameMap));
        }
        return list;
    }*/


}
